package com.imay.live.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.fragment.dynamics.DynamicsParams;
import com.imay.live.helper.UrlHelper;
import com.imay.live.model.FeedModel;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.utils.GlideUtil;


/**
 * 格子布局
 * Created by chan on 2017/3/25.
 */

public class DynamicsGridAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     */
    public DynamicsGridAdapter() {
        super(R.layout.item_dynamisc_grid, null);
    }

    int width = 0;
    int height = 0;

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        String imageUrl = null;
        boolean hasDel = false;
        if (item instanceof FeedModel) {
            FeedModel bean = (FeedModel) item;
            if (bean.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_IMAGE) {
                imageUrl = bean.getImgUrl() + "?imageslim";
                helper.setVisible(R.id.iv_discovery_item_video, false);
                helper.setVisible(R.id.iv_discovery_item_live, false);
            } else if (bean.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_VIDEO) {
                imageUrl = UrlHelper.getVideoThumbUrl(bean.getVideoUrl());
                helper.setVisible(R.id.iv_discovery_item_video, true);
                helper.setVisible(R.id.iv_discovery_item_live, false);
            }
            if (bean.getRepostedFeed() != null && bean.getRepostedFeed().getUid() == 0) {
                hasDel = true;
            } else {
                hasDel = false;
            }
            if (bean.getFamilyId() == 0 || bean.getInternalPublic() == 0) {
                helper.setVisible(R.id.iv_discovery_item_family, false);
            } else {
                helper.setVisible(R.id.iv_discovery_item_family, true);
            }
        } else if (item instanceof RoomInfoModel) {
            RoomInfoModel bean = (RoomInfoModel) item;
            if (bean.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_LIVE) {
                helper.setVisible(R.id.iv_discovery_item_video, false);
                helper.setVisible(R.id.iv_discovery_item_live, true);
                imageUrl = bean.getImgFace();
                if (TextUtils.isEmpty(imageUrl)) {
                    imageUrl = bean.getUser().getImgHead();
                }
            }
        }

        helper.getConvertView().getWidth();
        ImageView coverIv = helper.getView(R.id.iv_discovery_item_cover);
        helper.addOnClickListener(R.id.iv_discovery_item_cover);

        Object url;
        if (hasDel) {
            url = R.mipmap.ico_01_nodata;
        } else {
            url = imageUrl;
        }
        GlideUtil.loadRectangleImage(coverIv, url, R.dimen.size_4dp, R.drawable.shape_placeholder_2);
    }


}
